package com.baosight.sczx.zm.cg.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cg.domain.ZMCGDutyschedule;
import com.baosight.sczx.zm.cn.domain.ZMCNQtyMain;
/**
 * 排班
 * <p>Title: ServiceZMCG51.java</p> 
 */
public class ServiceZMCG51 extends ServiceEPBase {
	/**
	 * 初始化方法 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);  
		return outInfo;
	}
	/**
	 * 查询方法 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) { 
		StaticTools.setQueryParameters(inInfo, "fdMiningId",UserSession.getInSessionProperty("miningCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCGDutyschedule.query", new ZMCGDutyschedule());
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		return outInfo;
	}
 
	/**
	 * <p>Function: insert</p> 
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGDutyschedule _dom = new ZMCGDutyschedule();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));  
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_DUTYSCHEDULE"));
			this.dao.insert("ZMCGDutyschedule.insert",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	/**
	 * <p>Function: update</p> 
	 */
	@Override	
	public EiInfo update(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGDutyschedule _dom = new ZMCGDutyschedule();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i)); 
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdVirtualDate", _dom.getFdVirtualDate());
			boolean validate = this.validate(inInfo, _dom, params);
			if(validate){
				return inInfo;
			}
			this.dao.update("ZMCGDutyschedule.update",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	/**
	 * <p>Function: delete</p> 
	 */
	@Override	
	public EiInfo delete(EiInfo inInfo) { 
		EiInfo _temp = super.delete(inInfo, "ZMCGDutyschedule.delete");
		EiInfo out=new EiInfo(); 
		out = this.query(inInfo);  
		out.setMsg(_temp.getMsg());
		out.setDetailMsg(_temp.getDetailMsg());
		return out; 
	}
	
	/**
	 * 校验是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:48:34 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, ZMCGDutyschedule _dom, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdVirtualDate = _dom.getFdVirtualDate();
		
		params.put("fdVirtualDate", fdVirtualDate);
		
		List result = dao.query("ZMCGDutyschedule.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCGDutyschedule _temp = (ZMCGDutyschedule)result.get(0);
		
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdVirtualDate() != null && _temp.getFdVirtualDate().equals(fdVirtualDate)){
				msg.append(" 调度日期");
			}

		msg.append("重复！");
		
		inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
}
